package com.wyix;

import java.util.concurrent.CyclicBarrier;

/**
 * @author wangdx
 * @since 2025/8/4
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        // 3个线程到达后触发回调，可重复使用
        CyclicBarrier barrier = new CyclicBarrier(3, () ->
                System.out.println("所有线程已到达，开始下一步"));

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + "到达");
                    barrier.await(); // 等待其他线程
                    System.out.println(Thread.currentThread().getName() + "继续执行");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "线程" + i).start();
        }
    }
}